<?php
    /**
    * Get list of cases belongs to selected user
    */
    if (!defined('EXPONENT')) exit('');
    function _manage_cases_return_empty()
    {
        $ret_object = null;
        $ret_object->total = 0;
        $ret_object->rows=array(); 
        echo json_encode($ret_object);
        return;
    }

    if (exponent_users_isLoggedIn())
    {  
        $user_id=$user->id;
        if (exponent_users_isAdmin() ||  exponent_permissions_check(array("administrate","upload","delete","meeting","meeting_delete","meeting_assistant"),exponent_core_makeLocation("profilemodule","_syscore",""))){
            if (isset($_REQUEST['user_id']))
                $user_id = intval($_REQUEST['user_id']);
        }
        // select user
        $cuser = $db->selectObject("user", "id = {$user_id}");
        if (!isset($cuser->id))
        {
            _manage_cases_return_empty();
        }
        else
        {
            $sql_where = "user_id = {$user_id} ";


            // Build condition for filter
            if (isset($_REQUEST['search']))
                $sql_where.=" AND (name LIKE '%{$_REQUEST['search']}%')";

            // get search date type
            $date_type="bypost";
            if (isset($_REQUEST['datetype']))
            {
                if ($_REQUEST['datetype'] == "bypost" || $_REQUEST['datetype'] == "byupdate")
                    $date_type=$_REQUEST['datetype'];
            }

            if (isset($_REQUEST['datefrom']) && $_REQUEST['datefrom']!="")
            {
                $date_from=strtotime($_REQUEST['datefrom']);
                if ($date_type == "bypost")
                    $sql_where.=" AND (post_date>={$date_from})";
                else
                    $sql_where.=" AND (last_update>={$date_from})";
            }

            if (isset($_REQUEST['dateto']) && $_REQUEST['dateto']!="")
            {
                $date_to=strtotime($_REQUEST['dateto']);
                if ($date_type == "bypost")
                    $sql_where.=" AND (post_date<={$date_to})";
                else
                    $sql_where.=" AND (last_update<={$date_to})";
            }

            if (isset($_REQUEST['filetype']))
            {
                $type=$_REQUEST['filetype'];
                if ($type == "none")
                    $sql_where.=" ";
                if ($type == "any")
                    $sql_where.=" AND (file_video <> '' OR file_pdf <>'')";
                else if ($type == "pdf")
                        $sql_where.=" AND (file_pdf <>'')";
                    else if ($type == "video")
                            $sql_where.=" AND (file_video <>'')";
                        else if ($type == "nofile")
                                $sql_where.=" AND (file_video ='') AND (file_pdf='')";
            }


            $page=1;
            if (isset($_REQUEST['page']))
                $page=intval($_REQUEST['page']);
            $pageSize=10;

            if (isset($_REQUEST['rows']))
                $pageSize=intval($_REQUEST['rows']);
            $start=($page-1)*$pageSize;

            // sort
            $sort="";
            if (isset($_REQUEST['sortName'])){
                $sort = " ORDER BY {$_REQUEST['sortName']} {$_REQUEST['sortOrder']}";
            }

            $data=$db->selectObjects('profilemodule_cases',"{$sql_where} {$sort} LIMIT {$start},{$pageSize}");

            // preprocess some data
            foreach ($data as $k=>$v){
                $v->post_date_text = date('d/m/Y H:i:s',$v->post_date);
                $v->last_update_text = date('d/m/Y H:i:s',$v->last_update);
            }

            $ret_object = null;
            $ret_object->total = $db->countObjects('profilemodule_cases',"{$sql_where}");
            $ret_object->rows = $data; 
            echo json_encode($ret_object);
        }

    }

?>